/** */
package com.hotau.account;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.framework.FooterVisible;
import com.hotau.framework.IndexFragment;
import com.hotau.framework.NeedLogin;
import com.hotau.member.LoginActivateFragment;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;

/**
 * 账户管理
 * 
 * @author fengweili</br>1.0
 * @author liujia (liujia9234@126.com)1.1 2011-12-9 下午02:15:27
 * @version 1.1
 *          <ul>
 *          <li>1.0 创建</li>
 *          <li>1.1 功能实现</li>
 *          </ul>
 */
public class AccountManageFragment extends Fragment implements FooterVisible,
		OnClickListener, NeedLogin, IndexFragment {

	private Button manage_blance_btn = null;
	private Button manage_pay_btn = null;
	private Button manage_password_btn = null;
	public Button activate_btn_jihuo = null;

	private TextView manager_tv_usercode = null;
	private TextView manager_tv_username = null;
	private TextView manager_tv_activate = null;
	private TextView manager_tv_money = null;

	private boolean isFirst = false;
	private JSONObject json_data = null;
	private SharedPreferences.Editor  shareEdit = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initAllWidget();
		initAllEvent();
		if (MemberService.isLogin(getActivity())) {
			shareEdit = getActivity().getSharedPreferences("MEMBER_USER",0).edit();
			StringBuilder sb_edit_pwd = new StringBuilder();
			sb_edit_pwd.append("Form:token="+MemberService.getToken(getActivity()));
			sb_edit_pwd.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
			
			final HotauActivity csmbpActivity = (HotauActivity) getActivity();
			HttpRequestAsynTask account_info_task = new HttpRequestAsynTask(getActivity()){
				@Override
				protected void doPostExecute(String result) {
					try {
						JSONObject data = new JSONObject(result);
						if(data != null && data.getInt("result") == 1){
							shareEdit.putString("account_user_info", data.toString());
							shareEdit.commit();
						}else{
							//用户信息获取失败
							//DialogUtil.showDialog(csmbpActivity, "",data.getString("errorMsg") );
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						//DialogUtil.showDialog(csmbpActivity, "", "账号信息查询返回数据出错 ");
					}
					
					JSONObject userInfo = MemberService.getUserInfo(csmbpActivity);
					if (userInfo != null) {
						try {
							json_data = new JSONObject();
							json_data.put("accName", userInfo.getString("accName"));
							json_data.put("realName", userInfo.getString("realName"));
							json_data.put("status", userInfo.getInt("status"));
							json_data.put("amount", userInfo.getString("amount"));
							pullData(json_data);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							DialogUtil.showDialog(csmbpActivity, "", "获取用户信息失败",
									new Executable() {
										@Override
										public void execute() {
											// TODO Auto-generated method stub
											csmbpActivity.toHome();
										}
									});
						}
					} else {
						DialogUtil.showDialog(csmbpActivity, "", "获取用户信息失败",
								new Executable() {
									@Override
									public void execute() {
										// TODO Auto-generated method stub
										csmbpActivity.toHome();
									}
								});
					}
				}
			};
			account_info_task.setShowProgressDialog(false);
			account_info_task.execute(HotauConstants.ACCOUNT_QUERY_INFO,sb_edit_pwd.toString(),HttpUtil.UTF8_ENCODING);
			
			// if(MemberService.isActivate(getActivity()) &&
			// !MemberService.isFreezing(getActivity())){
			// //激活
			// if(isFirst){
			// pullData(json_data);
			// }else{
			// StringBuilder sb_edit_pwd = new StringBuilder();
			// sb_edit_pwd.append("Form:token="+MemberService.getToken(getActivity()));
			// sb_edit_pwd.append(";terminalCode="+"");
			// HttpRequestAsynTask account_info_task = new
			// HttpRequestAsynTask(getActivity()){
			// @Override
			// protected void doPostExecute(String result) {
			// try {
			// JSONObject data = new JSONObject(result);
			// if(data != null && data.getInt("result") == 1){
			// AccountManageFragment.this.json_data = data;
			// pullData(json_data);
			// Editor shareEdit =
			// csmbpActivity.getSharedPreferences("MEMBER_USER",0).edit();
			// shareEdit.putString("user_info", data.toString());
			// isFirst = true;
			// }else{
			// //用户信息获取失败
			// DialogUtil.showDialog(getActivity(),
			// "",data.getString("errorMsg") );
			// }
			// } catch (JSONException e) {
			// // TODO Auto-generated catch block
			// DialogUtil.showDialog(getActivity(), "", "账号信息查询返回数据出错 ");
			// }
			// }
			// };
			// account_info_task.execute(HNAPAYConstants.ACCOUNT_QUERY_INFO,sb_edit_pwd.toString(),HttpUtil.UTF8_ENCODING);
			// }
			// }else if(MemberService.isFreezing(getActivity())){
			// //冻结
			// json_data = new JSONObject();
			// try {
			// json_data.put("accName",
			// MemberService.getLastLoginUserCode(getActivity()));
			// json_data.put("realName", "");
			// json_data.put("status", "0");
			// json_data.put("amount", "");
			// pullData(json_data);
			// } catch (JSONException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			// }else{
			// //未激活
			// json_data = new JSONObject();
			// try {
			// json_data.put("accName",
			// MemberService.getLastLoginUserCode(getActivity()));
			// json_data.put("realName", "");
			// json_data.put("status", "2");
			// json_data.put("amount", "");
			// pullData(json_data);
			// } catch (JSONException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			//
			// }

		}
	}

	public void pullData(JSONObject data) {
		try {
			// 用户信息获取成功
			manager_tv_usercode.setText(data.getString("accName"));
			manager_tv_username.setText(data.getString("realName"));
			String status = data.getString("status");

			manager_tv_activate.setText(MemberService
					.getAccStatusName(getActivity()));
			
			if ("0".equals(status)) {
				activate_btn_jihuo.setVisibility(ImageButton.INVISIBLE);
				manage_blance_btn.setEnabled(false);
				manage_pay_btn.setEnabled(false);
				manage_password_btn.setEnabled(false);
			} else if ("1".equals(status)) {
				activate_btn_jihuo.setVisibility(ImageButton.INVISIBLE);
				manage_blance_btn.setEnabled(true);
				manage_pay_btn.setEnabled(true);
				manage_password_btn.setEnabled(true);
			} else if ("2".equals(status)) {
				activate_btn_jihuo.setVisibility(ImageButton.VISIBLE);
				manage_blance_btn.setEnabled(false);
				manage_pay_btn.setEnabled(false);
				manage_password_btn.setEnabled(false);
			}
			String amount = data.getString("amount");
			if (!"".equals(amount)) {
				manager_tv_money.setText("￥" + data.getString("amount"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			//DialogUtil.showDialog(getActivity(), "", "账户信息出错");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.account_manage_facade, null);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		HotauActivity csmbpActivity = (HotauActivity) getActivity();
		switch (v.getId()) {
		case R.id.account_manage_blance_btn:
			csmbpActivity.pushFragment("收支汇总", new BlanceFragment());
			break;
		case R.id.account_manage_pay_btn:
			csmbpActivity.pushFragment("付款管理", new PayManageFragment());
			break;
		case R.id.account_manage_password_btn:
			csmbpActivity.pushFragment("密码管理", new PasswordManageFragment());
			break;
		case R.id.account_activate_btn_jihuo:
			Bundle bundle = new Bundle();
			bundle.putBoolean("isRegister", false);
			LoginActivateFragment laf = new LoginActivateFragment();
			laf.setArguments(bundle);
			csmbpActivity.pushFragment("信息提示", laf);
			break;
		default:
			break;
		}
	}

	/**
	 * 初始化所有控件
	 */
	public void initAllWidget() {
		manage_blance_btn = (Button) getActivity().findViewById(
				R.id.account_manage_blance_btn);
		manage_pay_btn = (Button) getActivity().findViewById(
				R.id.account_manage_pay_btn);
		manage_password_btn = (Button) getActivity().findViewById(
				R.id.account_manage_password_btn);
		activate_btn_jihuo = (Button) getActivity().findViewById(
				R.id.account_activate_btn_jihuo);

		// manage_blance_btn.getPaint().setFakeBoldText(true);
		// manage_pay_btn.getPaint().setFakeBoldText(true);
		// manage_password_btn.getPaint().setFakeBoldText(true);

		manager_tv_usercode = (TextView) getActivity().findViewById(
				R.id.account_manager_tv_usercode);
		manager_tv_username = (TextView) getActivity().findViewById(
				R.id.account_manager_tv_username);
		manager_tv_activate = (TextView) getActivity().findViewById(
				R.id.account_manager_tv_activate);
		manager_tv_money = (TextView) getActivity().findViewById(
				R.id.account_manager_tv_money);
	}

	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
		manage_blance_btn.setOnClickListener(this);
		manage_pay_btn.setOnClickListener(this);
		manage_password_btn.setOnClickListener(this);
		activate_btn_jihuo.setOnClickListener(this);
	}

}
